import service from '@/utils/request';
// 获取环境cpu、内存信息 ==========================
export const getEnvironmentInfo = (id, env_type) =>
  service({
    url: `/verify/outline/?node_env_id=${id}&env_type=${env_type}`,
    method: 'get',
  });
  // 获取节点 ==========================
  export const getDescFunc = (id, node_id) =>
    service({
      url: `/verify/node_info/?id=${id}&node_id=${node_id}`,
      method: 'get',
    });

// 启动漏洞验证 ==========================
export const startVuln = data =>
  service({
    url: `/verify/start/`,
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });

// 获取漏洞验证任务列表 ==========================
export const getVulnTaskList = params => {
  if(params.start_time && params.end_time) {
    return service({
      url: `/verify/task/?page_index=${params.page_index}&page_size=${params.page_size}&name=${params.name}&start_time=${params.start_time}&end_time=${params.end_time}&vuln_title=${params.vuln_title}`,
      method: 'get',
    });
  } else {
    return service({
      url: `/verify/task/?page_index=${params.page_index}&page_size=${params.page_size}&name=${params.name}&vuln_title=${params.vuln_title}`,
      method: 'get',
    });
  }
}
// 删除漏洞验证任务 ==========================
export const deleteVulnTask = data =>
  service({
    url: `/verify/task/`,
    method: 'delete',
    data,
  }); 
// 漏洞验证详情 ==========================
export const getVulnTaskDetail = id =>
  service({
    url: `/verify/task_detail/?id=${id}`,
    method: 'get',
  });

  //获取画布详情 ==========================
export const getTopologyDetail = (id) =>
  service({ // 新接口
    url: `/verify/detail/?id=${id}`,
    method: 'get',
  });
/**
 * @description 获取漏洞验证环境列表
 * @param {Object} params - 查询参数
 * @returns {Promise} Promise 对象
 */
// 获取漏洞验证环境列表 ==========================
export function getVulnEnvironmentList(params) {
  return service({
    url: '/verify/env_info/',
    method: 'get',
    params
  })
}

/**
 * @description 启动漏洞验证环境
 * @param {Object} data - 操作数据
 * @returns {Promise} Promise 对象
 */
// 启动漏洞验证环境 ==========================
export function OperateVulnEnvironment(data) {
  return service({
    url: `/verify/operation/`,
    method: 'post',
    headers: {
      "Content-Type": "multipart/form-data",
    },
    data
  })
}

/**
 * @description 保存环境配置
 * @param {Object} data - 配置信息
 * @returns {Promise} Promise 对象
 */
// 保存环境配置 ==========================
export function saveEnvironmentConfig(data) {
  return service({
    url: '/api/vulnEnvironment/config/save',
    method: 'post',
    data
  })
}

/**
 * @description 获取环境配置
 * @returns {Promise} Promise 对象
 */
// 获取环境配置 ==========================
export function getEnvironmentConfig() {
  return service({
    url: '/api/vulnEnvironment/config',
    method: 'get'
  })
} 

/**
 * @description 获取漏洞验证任务监控数据
 * @param {string|number} taskId - 任务ID
 * @returns {Promise} Promise 对象
 */
// 获取漏洞验证任务监控数据 ==========================
export function getVulnVerificationMonitor(taskId) {
  return service({
    url: `/api/vulnEnvironment/monitor/${taskId}`,
    method: 'get'
  })
}

/**
 * @description 获取漏洞验证任务日志
 * @param {string|number} taskId - 任务ID
 * @returns {Promise} Promise 对象
 */
// 获取漏洞验证任务日志 ==========================
export function getVulnVerificationLogs(taskId) {
  return service({
    url: `/api/vulnEnvironment/logs/${taskId}`,
    method: 'get'
  })
}

//修改节点
export const editorPutNode = (data) =>
  service({
    url: `/verify/topo/`,
    method: 'put',
    data,
  });

/**
 * @description 控制环境虚拟机操作（启动/停止/重启）
 * @param {Object} data - 包含id, vm_id, operation字段的对象
 * @returns {Promise} Promise 对象
 */
// 控制环境虚拟机 ==========================
export const controlEnvVM = (data) =>
  service({
    url: `/verify/virtual_operation/`,
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });